$(function() {
	home.scrapHighChart(); //调用废钢铁曲线图
	home.paperHighChart(); //调用废纸曲线图
	home.applianceHighChart(); //调用废家电柱形图
	home.recommend(); //调用精选推荐
	home.yesterdayAjax(); //调用成交单数
	//钢铁 家电点击按钮搜索进入商城页面
	home.search_btn_click();

	//首页banner
	var mySwiper1 = new Swiper('.swiper-container1', {
		autoplay: 3000,
		speed: 300,
		loop: true,
		paginationClickable: true,
		// 如果需要分页器
		pagination: '.swiper-pagination',
	});
	//废纸
	var mySwiper3 = new Swiper('#swiper-container3', {
		// 如果需要前进后退按钮
		nextButton: '.swiper-button-next',
		prevButton: '.swiper-button-prev'
	});

});

var home = {
	//搜索模块
	search_btn_click: function() {
		// 钢铁，纸，家电切换
		$('#product_list').on('click', 'li', function() {
			var index = $(this).index();
			$(this).addClass('li_active').siblings().removeClass('li_active');
			$('.product_main').eq(index).show().siblings().hide();
		});
		/*家电规格根据不同的品名获取*/
		$('#product_ming2').on('change', function() {
			var pinming = $(this).val();
			home.home_applicance_size(pinming);
		});
		//钢铁 搜索 获取条件值传递到商城页面
		$('#search').click(function() {
			localCity = $('#product_city').val();
			species = $('#product_ming').val();
			caizhi = $('#input_type').val();
			steelMill = $('#input_text').val();
			sign = 1;
			window.location.href = 'steel.html?localCity=' + localCity + '&species=' + species + '&caizhi=' + caizhi + '&steelMill=' + steelMill + '&sign=' + sign;
		});
		//家电 搜索 获取条件值传递到商城页面
		$('#search2').click(function() {
			localCity = $('#product_city2').val();
			species = $('#product_ming2').val();
			size = $('#input_type2').val();
			disassembleFactory = $('#input_text2').val();
			sign = 2;
			window.location.href = 'steel.html?localCity=' + localCity + '&species=' + species + '&size=' + obj.size + '&disassembleFactory=' + disassembleFactory + '&sign=' + sign;
		});
	},

	/*家电规格根据不同的品名获取*/
	home_applicance_size: function(pinming) {
		if(!pinming) {
			pinming = '';
		};
		$.ajax({
			type: "post",
			url: "http://47.93.102.34:8088/cmscm/webshop/showSizeAp",
			async: true,
			data: {
				"species": pinming
			},
			success: function(ags) {
				var Data = ags[0].content;
				$('.gui').html("");
				$('.gui').append('<option value="">请选择</option>');
				for(var i = 0; i < Data.length; i++) {
					$('.gui').append('<option value="' + Data[i] + '">' + Data[i] + '</option>');
				};
			}
		});
	},
	//数字转化整数
	translateInt: function(float_num) {
		return Math.round(float_num);
	},
	//废铁
	scrapHighChart: function() {
		var that = this;
		var dataArr = []; //y轴数据 所有钢铁类的平均值
		var timeArr = []; //x轴数据 时间
		var speciesList = []; //左边数据数组
		var splist = [];
		//废铁Ajax
		$.ajax({
			url: 'http://47.93.102.34:8088/cmscm/webshop/showShouYeGT?type=1',
			type: 'post',
			async: true,
			dataType: 'json',
			success: function(res) {
				if(res.ret == -1) {
					alert('接口请求失败！');
					return false;
				} else {
					var data = res[0].content; //data是个数组
				};
				//循环日期和价格数据到数组
				$.each(data, function(i, v) {
					//日期数组
					timeArr.push(this.monthTime.slice(5));
					//循环该月份的所有种类的钢铁价格求平均值
					var this_month_total_pirce = 0; //该月份的总价
					var this_month_kinds_nums = 0; //该月份种类数量
					$.each(this.speciesList, function(index, value) {
						this_month_total_pirce += value.avg;
						this_month_kinds_nums = index + 1;
					});
					//该月份平均值取整数 放到数组的最后面
					dataArr.unshift(that.translateInt(this_month_total_pirce / this_month_kinds_nums));
				});
				//hightcharts曲线图
				highchartFn(timeArr, dataArr);
				//左边数据表
				//循环数据渲染页面
				$.each(data, function(i, v) {
					var $slide_el = $('<div class="swiper-slide"></div>');
					//循环一个月的数据
					var all_col_el = '';
					$.each(v.speciesList, function(index, value) {
						var col_el = '<div class="main_list_flex"><p class="main_list_money"><span class="main_list_red">' +
							value.avg + '</span><span>&nbsp元</span></p><p class="main_list_text">' + value.speciesName + '</p></div>';
						all_col_el += col_el;
					});
					$slide_el.append(all_col_el);
					$('#swiper_feitie').append($slide_el);
				});
				var mySwiper2 = new Swiper('#swiper-container2', {
					nextButton: '.swiper-button-next',
					prevButton: '.swiper-button-prev'
				});
			}
		});

		function highchartFn(timeArr, dataArr) {
			$('#scrap').highcharts({
				axisLabel: {
					interval: 0
				},
				title: {
					text: '废钢铁指数（元/吨）',
					align: 'left',
					style: {
						fontSize: '17px'
					},
					y: 6
				},
				xAxis: {
					type: 'datetime',
					labels: {
						format: '{value: %m 月 }',
						align: 'center',
						rotation: 0
					}
				},
				yAxis: {
					title: {
						text: null
					}
				},
				plotOptions: {
					spline: {
						lineWidth: 2,
						states: {
							hover: {
								lineWidth: 3
							}
						},
						marker: {
							enabled: false
						},
					}
				},
				credits: {
					enabled: false
				},
				legend: { //方框所在的位置(不知道怎么表达)  
					layout: 'vertical',
					verticalAlign: 'top',
					x: 190,
					y: -14,
					borderWidth: 0
				},
				series: [{
					name: '<b style="color:#333333;font-weight:100;font-size:15px">废钢铁</b>',
					data: dataArr,
					pointStart: Date.UTC(2017, (timeArr[timeArr.length - 1] - 1), 1),
					pointInterval: 31 * 24 * 3600 * 1000
				}, ],
				navigation: {
					menuItemStyle: {
						fontSize: '10px'
					}
				}
			});
		};
	},
	//废纸曲线图
	paperHighChart: function() {

		$('#paper').highcharts({
			chart: {
				// type: 'spline'
			},
			title: {
				text: '废纸指数（元/吨）',
				align: 'left',
				style: {
					fontSize: '17px'
				},
				y: 6
			},
			xAxis: {
				type: 'datetime',
				labels: {
					format: '{value: %m 月 }',
					align: 'center',
					rotation: 0
				}
			},
			yAxis: {
				title: {
					text: null
				}
			},

			plotOptions: {
				spline: {
					lineWidth: 2,
					states: {
						hover: {
							lineWidth: 3
						}
					},
					marker: {
						enabled: false
					}
				}
			},
			credits: {
				enabled: false
			},
			legend: { //方框所在的位置(不知道怎么表达)  
				layout: 'vertical',
				verticalAlign: 'top',
				x: 190,
				y: -14,
				borderWidth: 0
			},
			series: [{
				name: '<b style="color:#333333;font-weight:100;font-size:15px">废纸</b>',

				data: [2190, 2100, 2180, 2130, 2110, 2190],
				pointStart: Date.UTC(2017, 3, 1),
				pointInterval: 31 * 24 * 3600 * 1000
			}, ],
			navigation: {
				menuItemStyle: {
					fontSize: '10px'
				}
			}
		});
	},
	//废家电曲线图
	applianceHighchartFn: function(xAxisLable, dataArr, month) {
		$('#appliance').highcharts({
			chart: {
				type: 'column'
			},
			title: {
				text: '废家电指数（元/台）',
				align: 'left',
				style: {
					fontSize: '17px'
				},
				y: 6
			},
			subtitle: {
				text: month + '月份数据',
				align: 'right'
			},
			xAxis: {
				type: 'categories',
				categories: xAxisLable
			},
			yAxis: {
				title: {
					text: null
				}
			},
			plotOptions: {
				spline: {
					lineWidth: 2,
					states: {
						hover: {
							lineWidth: 3
						}
					},
					marker: {
						enabled: false
					}
				}
			},
			credits: {
				enabled: false
			},
			legend: { //方框所在的位置(不知道怎么表达)  
				layout: 'vertical',
				verticalAlign: 'top',
				x: 190,
				y: -14,
				borderWidth: 0
			},
			series: [{
				name: '<b style="color:#333333;font-weight:100;font-size:15px">家电</b>',
				data: dataArr,
			}],
			navigation: {
				menuItemStyle: {
					fontSize: '10px'
				}
			}
		});
	},
	//废家电
	applianceHighChart: function() {
		var that = this;
		var priceArr = []; //y轴数据 一个种类家电所有规格的平均值
		var xAxisLable = []; //x轴数据 家电种类
		var speciesList = []; //左边数据数组
		var splist = [];
		$.ajax({
			url: 'http://47.93.102.34:8088/cmscm/webshop/showShouYeJD',
			type: 'post',
			async: true,
			dataType: 'json',
			success: function(res) {
				if(res.ret == -1) {
					alert('接口请求失败！')
					return false;
				} else {
					var result = res[0].content;
				};
				//取最新月份的数据
				var nowMonth = result[0].monthTime.slice(5);
				var price = 0;
				$.each(result[0].speciesList, function(index, value) {
					xAxisLable.push(value.species);
					$.each(value.sizeList, function(i, v) {
						price += v.price;
					});
					priceArr.push(that.translateInt(price / value.sizeList.length));
				});
				that.applianceHighchartFn(xAxisLable, priceArr, nowMonth);
				//左边数据表
				//循环数据渲染页面(循环死了 我都看不懂了)
				var slide_el_all = '';
				$.each(result, function(i, v) {
					var slide_el = '<div class="swiper-slide"><ul class="jd_ul">';
					var col_el_all = '';
					$.each(v.speciesList, function(index, value) {
						var jd_type_title = value.species;
						var col_el = '<li><p class="jd_type_title">' + value.species + '</p><div class="jd_col_content">';
						var price_size_el = '';
						$.each(value.sizeList, function(ind, val) {
							if(ind == 2) {
								return false;
							};
							var price = val.price;
							var size = val.size;
							var a_price_size_el;
							a_price_size_el = '<p><span class="jd_size">' + size + '</span><span class="jd_price_wrap"><span>' +
								price + '</span>元/台</span></p>';
							price_size_el += a_price_size_el;
						});
						col_el_all += col_el + price_size_el + '</div></li>';
					});
					slide_el_all += slide_el + col_el_all + '</ul></div>';
				});

				$('#swiper_jiadian').append(slide_el_all);
				var mySwiper4 = new Swiper('#swiper-container4', {
					// 如果需要前进后退按钮
					nextButton: '.swiper-button-next',
					prevButton: '.swiper-button-prev'
				});
			}
		});
	},
	//精选推荐
	recommend: function() {
		var list = '';
		$.ajax({
			url: 'http://47.93.102.34:8088/cmscm/rank/showStatBySpeciesGT',
			type: 'post',
			async: false,
			dataType: 'json',
			success: function(res) {
				if(res.ret==-1){
					alert('接口请求失败！');
				}else{
					var data = res[0].content;
				};
				if(data.length != 0) {
					for(var i = 0; i < data.length; i++) {
						list += '<div class="main_product">' +
							'<span>' +
							'<img src="images/good.png" alt="">' +
							'</span>' +
							'<span class="main_pro_name">' +
							data[i].speciesName +
							'</span>' +
							'<span class="main_pro_type">钢材</span>' +
							'<span class="main_pro_num">' +
							data[i].price +
							'</span>' +
							'</div>'
					}
					$('#product_type').empty().html(list)
				} else {
					$('#product_type').html('<img class="nodata_img" src="images/nodata.jpg"/>')
				};
			}
		});
	},
	//成交单数
	yesterdayAjax: function() {
		var list = '';
		$.ajax({
			url: 'http://47.93.102.34:8088/cmscm/rank/showIndexRightStat',
			timeout: 1000, //超时时间设置，单位毫秒
			type: 'post',
			async: true,
			dataType: 'json',
			complete: function(XMLHttpRequest, status) {　
				if(status == 'timeout') {　
					var nodate_pic = '<img class="nodata_img" style="height: 64%" src="images/nodata.jpg"/>';　　　　
					$('.main_pic_result').append(nodate_pic);
				}　　
			},
			success: function(res) {
				if(res.ret==-1){
					alert('接口请求失败!');
				}else{
					var data = res[0].content;
				};
				$zr_li1 = $('<div class="main_result_box main_result_box1 p-10 zr_li1">' +
					'<p class="main_result_people">' +
					'昨日成交' +
					'</p>' +
					'<p class="main_result_parent">' +
					'<span class="main_result_num" id="yesterdayDan">' + 460 + '</span>' +
					'<span class="main_result_dan">单</span>' +
					'</p>' +
					'</div>');
				$('.main_pic_result').append($zr_li1);
				$main_result_flex = $('<div class="main_result_box p-10 main_result_flex Bord1 zr_li2">' +
					'<div class="main_result_panel Bord">' +
					'<p class="main_result_people">' +
					'<span class="main_result_type">' +
					'钢铁' +
					'</span>' +
					'<span class="main_result_zuo">' +
					'成交量' +
					'</span>' +
					'</p>' +
					'<p class="main_result_parent">' +
					'<span class="main_result_num1" id="totalSteel">' + 3848 + '</span>' +
					'<span class="main_result_dan">吨</span>' +
					'</p>' +
					'<div class="Bord3"></div>' +
					' </div>' +
					'<div class="main_result_panel">' +
					'<p class="main_result_people">' +
					'<span class="main_result_type">' +
					'纸' +
					'</span>' +
					'<span class="main_result_zuo">' +
					'成交量' +
					'</span>' +
					'</p>' +
					'<p class="main_result_parent">' +
					'<span class="main_result_num1" id="totalPaper">' + data.totalPaper + '</span>' +
					' <span class="main_result_dan">吨</span>' +
					'</p>' +
					'</div>' +
					'<div class="Bord2"></div>' +
					'</div>')
				$('.main_pic_result').append($main_result_flex);
			}
		});
	}

};